<script lang="ts" setup>

import { ref } from 'vue'
import { genFileId } from 'element-plus'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'

const url = 'http://localhost:8080/api/auth/upload'

const upload = ref<UploadInstance>()

const handleExceed: UploadProps['onExceed'] = (files) => {
  upload.value!.clearFiles()
  const file = files[0] as UploadRawFile
  file.uid = genFileId()
  upload.value!.handleStart(file)
}

const submitUpload = () => {
  upload.value!.submit()
}

</script>

<template>
  <div class="ordering">

    <el-upload
        ref="upload"
        class="upload-demo"
        :action="url"
        :limit="1"
        :on-exceed="handleExceed"
        :auto-upload="false"
    >
      <template #trigger>
        <el-button type="primary">选择图片</el-button>
      </template>
      <el-button class="ml-3" type="success" @click="submitUpload">
        上传
      </el-button>
      <template #tip>
        <div class="el-upload__tip text-red">
          限制一个图片，新的文件会覆盖上一个文件
        </div>
      </template>
    </el-upload>

  </div>
</template>

<style scoped>

</style>